package com.hh.service.service.impl.user;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hh.common.model.integral.UserPointRecord;
import com.hh.common.model.user.User;
import com.hh.common.model.user.UserSmallChangeRecord;
import com.hh.common.vo.user.UserAssetVo;
import com.hh.service.dao.UserDao;
import com.hh.service.dao.UserSmallChangeRecordDao;
import com.hh.service.dao.integral.UserPointRecordDao;
import com.hh.service.service.UserService;
import com.hh.service.service.user.UserAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class UserAssetServiceImpl implements UserAssetService {

    @Resource
    private UserPointRecordDao userPointRecordDao;

    @Resource
    private UserDao userDao;

    @Resource
    private UserSmallChangeRecordDao userSmallChangeRecordDao;

    @Autowired
    private UserService userService;



    /**
     * 更新用户积分
     * @param userId
     * @param price
     * @param type
     * @param transactionType
     * @param orderType
     * @param orderNo
     * @return
     */
    @Override
    public Boolean updateUserIntegralBalance(Integer userId, Integer price, String type, Integer transactionType, Integer orderType, String orderNo) {
        Integer pm = 1;
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        if (type.equals("add")) {
            updateWrapper.setSql(StrUtil.format("integral = integral + {}", price));
        } else {
            pm = 2;
            updateWrapper.setSql(StrUtil.format("integral = integral - {}", price));
            updateWrapper.last(StrUtil.format(" and (integral - {} >= 0)", price));
        }
        updateWrapper.eq("uid", userId);
        boolean updateBo = userService.update(updateWrapper);
        if(updateBo){
            UserAssetVo userAsset = userService.getUserAsset(userId);
            Integer newIntegral = userAsset.getIntegral();
            UserPointRecord userPointRecord = new UserPointRecord();
            userPointRecord.setUid(userId);
            userPointRecord.setTurnover(price);
            userPointRecord.setBalance(newIntegral);
            userPointRecord.setPm(pm);
            userPointRecord.setTransactionType(transactionType);
            if(null != orderType && null != orderNo){
                userPointRecord.setOrderType(orderType);
                userPointRecord.setOrderId(orderNo);
            }
            userPointRecordDao.insert(userPointRecord);
        }
        return Boolean.TRUE;
    }

    /**
     * 更新用户零钱
     * @param uid
     * @param price
     * @param type
     * @param transactionType
     * @param userRechargeId
     * @return
     */
    @Override
    public Boolean operationNowMoney(Integer uid, BigDecimal price, String type, Integer transactionType, Integer userRechargeId) {
        Integer pm = 1;
        int updateRes = 0;
        if (type.equals("add")) {
            updateRes = userDao.updateAddNowMoney(uid,price);
            //updateWrapper.setSql(StrUtil.format("now_money = now_money + {}", price));
        } else {
            pm = 2;
//            updateWrapper.setSql(StrUtil.format("now_money = now_money - {}", price));
//            updateWrapper.last(StrUtil.format(" and (now_money - {} >= 0)", price));
            updateRes = userDao.updateSubtractNowMoney(uid,price);
        }

        if(updateRes > 0){
            UserAssetVo userAsset = userService.getUserAsset(uid);
            BigDecimal newNowMoney = userAsset.getNowMoney();
            UserSmallChangeRecord userSmallChangeRecord = new UserSmallChangeRecord();
            userSmallChangeRecord.setUid(uid);
            userSmallChangeRecord.setTurnover(price);
            userSmallChangeRecord.setPm(pm);
            userSmallChangeRecord.setBalance(newNowMoney);
            userSmallChangeRecord.setTransactionType(transactionType);
            if(null != userRechargeId){
                userSmallChangeRecord.setUserRechargeId(userRechargeId);
            }
            userSmallChangeRecordDao.insert(userSmallChangeRecord);
        }
        return Boolean.TRUE;
    }
}
